/**
 * @author Mario, Tran
 */
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.ComponentOrientation;
import java.awt.Container;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.*;
import java.util.Scanner;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import sun.awt.WindowClosingListener;

/*
 * The method for implementing our GUI via cardlayout is to create a single JPanel which uses the cardlayout, call it cardStack for example
 * in our driving class (in this example cardLayoutTest) we have our cardStack and we create and add every JPanel accessible throughout our program 
 * directly into cardStack. Before/After inserting each "card" (JPanel) into cardStack be sure to add click listeners to the (button,or w/e) that will allow
 * us to traverse back and forth from that panel. Labeling each panel when adding it to the cardStack makes it easier to quickly tell our cardStack 
 * which card to display
 * 
 * heres an example i put together that is more applicable to our specific needs
 * 
 * a tutorial for cardlayout can be found here
 * http://download.oracle.com/javase/tutorial/uiswing/layout/card.html
 * 
 */

public class cardLayoutTest implements ActionListener{
	//ignore teach for time being
	boolean b = false;
	JPanel cards;
	String Login = "Login panel";
	String mainMenu = "Menu panel";
	String speedTest = "WPM mode";
	String TeacherMode = "Teacher Mode";
	String CreateAcc = "Create panel";
	
	public boolean loggedin = false;
    public String[] newInfo = new String[100];
    public User user = new User();
    int filePointer = 0;
    int fileLength = 0;
    int phraseCount = 1;
	String adminVocab = "Vocabulary Admin";
	String createVocab = "Vocabulary Mode 2";
	
	//since im using actionlistener i have to declare my panels as class values/properties/ w.e. its called
	panel1 card1;
	panel2 card2;
	Rec card3;
	//TeacherModeGui card4;
	Admin_teacher card4;
	panelcreate card5;
	public class closer extends JFrame implements WindowListener{
		
		public closer(String s){
			super(s);
			addWindowListener(this);
		}

		@Override
		public void windowActivated(WindowEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void windowClosed(WindowEvent arg0) {
			// TODO Auto-generated method stub
			//System.out.println("SURPRISE CLOSE");
			if(phraseCount>1)
				user.userLogout();
		}

		@Override
		public void windowClosing(WindowEvent arg0) {
			// TODO Auto-generated method stub
			//System.out.println("SURPRISE CLOSE");
			if(phraseCount>1)
				user.userLogout();
		}

		@Override
		public void windowDeactivated(WindowEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void windowDeiconified(WindowEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void windowIconified(WindowEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void windowOpened(WindowEvent arg0) {
			// TODO Auto-generated method stub
			
		}
		
	}
	Admin_vocab card6;
	CreateVocab card7;
	
	public void addComponentToPane(Container pane){
		
        
		clickListener c = new clickListener();
		pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
         
        //Create the "cards".
        card1 = new panel1();
        card1.login.addActionListener(c);
        card1.create.addActionListener(c);
        card1.teacher.addActionListener(c);
        card1.student.addActionListener(c);
        //card1.student.addItemListener(l);
        
        card2 = new panel2();
        card2.time.stop();//avoid time setting off actions needlessly
        card2.LogOut.addActionListener(c);
        card2.WPM.addActionListener(c);
        card2.vocab.addActionListener(c);
        
        card3 = new Rec();
        card3.mainMenu.addActionListener(c);
        card3.reset.addActionListener(c);
        
        
        //card4 = new TeacherModeGui();
        //card4.btnPrevious.addActionListener(c);
        card4 = new Admin_teacher();
        card4.prev.addActionListener(c);
        card4.manage_vocab.addActionListener(c);
        
        card5 = new panelcreate();
        //card5.create.addActionListener(c);
        card5.backmenu.addActionListener(c);
        
        card6 = new Admin_vocab();
        card6.back.addActionListener(c);
        card6.createV.addActionListener(c);
        
        card7 = new CreateVocab();
        card7.backV.addActionListener(c);
        
        //Create the panel that contains the "cards".
        cards = new JPanel(new CardLayout());
        cards.add(card1, Login);
        cards.add(card2, mainMenu);
        cards.add(card3, speedTest);
        cards.add(card4, TeacherMode);
        cards.add(card5, CreateAcc);
        cards.add(card6, adminVocab);
        cards.add(card7, createVocab);
        pane.add(cards, BorderLayout.CENTER);
	}
	
	
	
	private static void createAndShowGUI() {
        //Create and set up the window.
        cardLayoutTest woo = new cardLayoutTest();
        cardLayoutTest.closer frame = woo.new closer("Typin Tron 9001 proto");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 600);
         
        ImageIcon Tsymbol = new ImageIcon("T.png");
        Image T = Tsymbol.getImage();
        
        frame.setIconImage(T);
        //Create and set up the content pane.
        cardLayoutTest test = new cardLayoutTest();
        test.addComponentToPane(frame.getContentPane());
       
        //Display the window.
        //frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setResizable(false);
    }
	
	public static void main(String[] args) {
        /* Use an appropriate Look and Feel */
        JFrame.setDefaultLookAndFeelDecorated(true);
        //delete this block of code, its unneeded
           /* try {
            	UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
				//UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InstantiationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (UnsupportedLookAndFeelException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}*/
        
        /* Turn off metal's use of bold fonts */
        //UIManager.put("swing.boldMetal", Boolean.FALSE);
         
        //Schedule a job for the event dispatch thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
	
	
	
	private class clickListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			if(e.getSource() == card2.LogOut){
				user.userLogout();
				user.wpmIndex = 0;
				
				CardLayout cl = (CardLayout)(cards.getLayout());
		        cl.show(cards, Login);
		        card2.time.stop();
			}
			if(e.getSource() == card1.login){
				CardLayout cl = (CardLayout)(cards.getLayout());
					String uname = card1.loginUserName.getText();
				    String upw = card1.loginUserPw.getText();
				    String teachIP = card1.ip.getText();
				    user.userLogin(uname, upw, teachIP);
		        cl.show(cards, mainMenu);
		        card2.time.start();
			}
			if(e.getSource() == card1.create)
			{
				CardLayout cl = (CardLayout)(cards.getLayout());
				cl.show(cards, CreateAcc);
				//System.out.println("This is working");
			}
		    if(e.getSource() == card2.WPM){
				CardLayout cl = (CardLayout)(cards.getLayout());
		        cl.show(cards, speedTest);
		        card3.typeThis.requestFocus();
		        card2.time.stop();
			
			}
		    
		    if(e.getSource() == card3.mainMenu){
		    	CardLayout cl = (CardLayout)(cards.getLayout());
				String sessionWPM = card3.WPM();
		    	phraseCount++;
		    	if(!sessionWPM.equals("0.0")){
		    		//System.out.println("You pressed Quit!");
		    		System.out.println("WPM for session "+(user.wpmIndex+1)+" is "+sessionWPM);
		    		user.allWPM[user.wpmIndex] = Double.parseDouble(sessionWPM);
		    		System.out.println("Data saved in allWPM["+user.wpmIndex+"] is "+user.allWPM[user.wpmIndex]);
		    		user.wpmIndex++;}
		    	cl.show(cards, mainMenu);
		    	card2.time.start();
		    }
		    
		    if(e.getSource() == card3.reset){
		    	CardLayout cl = (CardLayout)(cards.getLayout());
		    	String sessionWPM = card3.WPM();
		    	phraseCount++;
		    	if(!sessionWPM.equals("0.0")){
		    		//System.out.println("You pressed reset!");
		    		System.out.println("WPM for session "+(user.wpmIndex+1)+" is "+sessionWPM);
		    		user.allWPM[user.wpmIndex] = Double.parseDouble(sessionWPM);
		    		System.out.println("Data saved in allWPM["+user.wpmIndex+"] is "+user.allWPM[user.wpmIndex]);
		    		user.wpmIndex++;}
		    }
		    if(e.getSource() == card2.vocab){
		    	CardLayout cl = (CardLayout)(cards.getLayout());
		    	cl.show(cards, TeacherMode);
		    	card2.time.stop();
		    }
		    /*
		    if(e.getSource() == card4.btnPrevious){
		    	CardLayout cl = (CardLayout)(cards.getLayout());
		    	cl.show(cards, mainMenu);
		    	card2.time.start();
		    }*/
		    if(e.getSource() == card4.prev)
		    {
		    	CardLayout cl = (CardLayout)(cards.getLayout());
		    	cl.show(cards, mainMenu);
		    }
		    if(e.getSource() == card4.manage_vocab)
		    {
		    	CardLayout cl = (CardLayout)(cards.getLayout());
		    	cl.show(cards, adminVocab);
		    }
		    
		    if(e.getSource() == card5.backmenu)
		    {
		    	CardLayout cl = (CardLayout)(cards.getLayout());
		    	cl.show(cards, Login);
		    }
		    
					    if(e.getSource() == card5.createNew)
		    {
		    	String userName, userPassword1, userPassword2;
		    	userName = card5.username.getText();
		    	userPassword1 = card5.userpw.getText();
		    	userPassword2 = card5.userpw_re.getText();
		    	
		    	if(userPassword1.equals(userPassword2)){
		    		FileWriter fstream;
					BufferedWriter out;
					try {
						fstream = new FileWriter("NetworkingStuff.txt");
						out = new BufferedWriter(fstream);
						out.write(userName);
						out.newLine();
						out.write(userPassword1);
						out.newLine();
						out.write(card1.ip.getText());
						out.newLine();
						out.close();
					} catch (IOException e2) {
						e2.printStackTrace();
					}
					System.out.println("Account successfully created!");
		    		CardLayout cl = (CardLayout)(cards.getLayout());
		    		cl.show(cards, Login);
		    	}
		    	else if(!userPassword1.equals(userPassword2)){
		    		System.out.println("Passwords do not match!");
		    		System.out.println("Sorry, could not create account.");
		    	}
		    }
		    if(e.getSource() == card6.back)
		    {
		    	CardLayout cl = (CardLayout)(cards.getLayout());
		    	cl.show(cards, TeacherMode);
		    }
		    
		    if(e.getSource() == card6.createV)
		    {
		    	CardLayout cl = (CardLayout)(cards.getLayout());
		    	cl.show(cards, createVocab);
		    }
		    
		    if(e.getSource() == card7.backV)
		    {
		    	CardLayout cl = (CardLayout)(cards.getLayout());
		    	cl.show(cards, adminVocab);
		    }

		    if(card1.teacher.isSelected())
		    {
		    	card1.ip.setVisible(false);
		    	card1.ip_label.setVisible(false);
		    	
		    }
		    if(card1.student.isSelected())
		    {
		    	card1.ip.setVisible(true);
		    	card1.ip_label.setVisible(true);
		    }
		}
	}



	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		
	}




}
